/**
 *
 */
package oas.web;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import oas.db.LeaveRepository;
import oas.domain.Leave;
import oas.domain.User;

/**
 * 管理请假Controller
 *
 * @author jtc
 * @version v1.3.0
 */
@Controller
@RequestMapping("/manager")
public class LeaveController {
    private final static Log log = LogFactory.getLog(UserController.class);
    @Autowired
    private LeaveRepository leaveRepository;

    /**
     * leave
     *
     * @return 请假视图
     */
    @RequestMapping(value = "/leave", method = RequestMethod.GET)
    public String leaveList(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, Model model, HttpSession session) {

        User handler = (User) session.getAttribute("user");
        PaginationSupport<Leave> ps = leaveRepository.findPageManager(handler, pageNo, pageSize);
        model.addAttribute(ps);
        return "leaveList";
    }

    /**
     * 通过审核
     *
     * @param id 待通过请假的id
     * @return leaveList.jsp
     */
    @RequestMapping(value = "/leave/checkTrue", method = GET)
    public String checkLeaves(@RequestParam(value = "id", defaultValue = "") Long id, HttpSession session) {
        User handler = (User) session.getAttribute("user");
        leaveRepository.checkLeave(handler, id, true);
        return "redirect:/manager/leave";
    }

    /**
     * 不通过审核
     *
     * @param id 待通过请假的id
     * @return leaveList.jsp
     */
    @RequestMapping(value = "/leave/checkFalse", method = GET)
    public String checkLeavesFalse(@RequestParam(value = "id", defaultValue = "") Long id, HttpSession session) {

        User handler = (User) session.getAttribute("user");
        leaveRepository.checkLeave(handler, id, false);
        System.out.println("dfaskflaj");
        return "redirect:/manager/leave";
    }

    /**
     * 查看单条未通过请假详情
     *
     * @param id 待通过请假的id
     * @return leaveList.jsp
     */
    @RequestMapping(value = "/leave/{id}", method = GET)
    public String checkLeave(@PathVariable("id") Long id, HttpSession session, Model model) {

        model.addAttribute(leaveRepository.findOne(id));
        return "leave";
    }

    /**
     * 查看已通过审核的请假
     *
     * @param id 通过请假的id
     * @return leaveChecked.jsp
     */
    @RequestMapping(value = "/leave/leaveChecked", method = GET)
    public String leaveChecked(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, Model model, HttpSession session) {
        User handler = (User) session.getAttribute("user");
        PaginationSupport<Leave> ps = leaveRepository.findPageCheckedManager(handler, pageNo, pageSize);
        model.addAttribute("paginationSupportChecked", ps);
        return "leaveChecked";
    }

    /**
     * 查看单条请假详情
     *
     * @param id 待通过请假的id
     * @return leaveList.jsp
     */
    @RequestMapping(value = "/leave/leaveChecked/{id}", method = GET)
    public String leaveChecked(@PathVariable("id") Long id, HttpSession session, Model model) {
        model.addAttribute(leaveRepository.findOneChecked(id));
        return "leave";
    }

}
